package com.wafer.db;


import java.sql.*;

public class DBTest {

    public static void main(String[] args) {
        //声明Connection对象
        Connection con;
        //驱动程序名
        String driver = "com.mysql.cj.jdbc.Driver";
        //URL指向要访问的数据库名mydata
        String url = "jdbc:mysql://localhost:3306/visitor";
        //MySQL配置时的用户名
        String user = "root";
        //MySQL配置时的密码
        String password = "root";
        //遍历查询结果集
        try {
            //加载驱动程序
            Class.forName(driver);
            //1.getConnection()方法，连接MySQL数据库！！
            con = DriverManager.getConnection(url, user, password);
            if (!con.isClosed()) {
                System.out.println("Succeeded connecting to the Database!");
            }
            //2.创建statement类对象，用来执行SQL语句！！
            Statement statement = con.createStatement();
            //要执行的SQL语句
            String sql = "select * from visitor_health_survey ";
            //3.ResultSet类，用来存放获取的结果集！！
            ResultSet rs = statement.executeQuery(sql);
            System.out.println("--------------------------------------");
            System.out.println("执行结果如下所示:");
            System.out.println("------------------------");
            System.out.println("id" + "\t" + "内容" + "\t" + "办公地" + "\t" + "租户");
            System.out.println("--------------------------------------");
            String id;
            String content;
            String locationManageId;
            String companyId;
            String length;
            while (rs.next()) {
                //获取sno这列数据
                id = rs.getString("id");
                //获取sname这列数据
                content = rs.getString("content");
                //获取sex这列数据
                locationManageId = rs.getString("location_manage_id");
                //获取age这列数据
                companyId = rs.getString("company_id");
                //输出结果
                System.out.println(id + "\t" + content + "\t" + locationManageId + "\t" + companyId);
            }
            DatabaseMetaData metaData = con.getMetaData();
            String catalog = con.getCatalog();
            String schemaPattern = con.getMetaData().getUserName();
            //获取列信息
            rs = metaData.getColumns(catalog, schemaPattern, "sys_label", "%");
            while (rs.next()) {
                System.out.println(rs.getString("COLUMN_NAME") + "\t" + rs.getString("TYPE_NAME") + "\t"
                        + rs.getString("COLUMN_SIZE") + "\t" + rs.getString("REMARKS")+ "\t" + rs.getString("NULLABLE"));
            }
            rs.close();
            con.close();
        } catch (ClassNotFoundException e) {
            //数据库驱动类异常处理
            System.out.println("Sorry,can`t find the Driver!");
            e.printStackTrace();
        } catch (SQLException e) {
            //数据库连接失败异常处理
            e.printStackTrace();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        } finally {
            System.out.println("数据库数据成功获取！！");
        }
    }
}